Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use only the lowest 32 bit of input for 32 bit division operations #100

Merged
merged 2 commits into from
Jan 15, 2025

Conversation

mininny
Copy link
Collaborator

@mininny mininny commented Dec 10, 2024

Description

The RISCV implementations (sol/fast/slow) return incorrect results, in conflict with the RISCV specification, for all 32-bit division operations (DIVW, DIVUW, REMW, REMUW) when the divisor is zero.

All word (32-bit) arithmetic operations are supposed to take as its operands only the lowest 32 bits of the input registers.

However, in the division by zero code that requirement fails.

Observe that the switch statement only catches the case in which the input register has all bits set to zero. In reality, only the least significant 32 bits should be considered.

In the case any of the most significant 32 bits is set, the switch statement will follow the default path, and the division will be performed with the same behavior as an EVM division (except for sign extending) -- that means the division by zero will result in a zero result, instead of the correct RISCV one (all bits set for division, or the original numerator for remainder).

Mask the input before the switch statement.

@BlocksOnAChain BlocksOnAChain added the Audit finding grouping for our audit findings label Dec 16, 2024
Copy link
Contributor

@refcell refcell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Can we add tests for this?

@mininny
Copy link
Collaborator Author

mininny commented Jan 9, 2025

Looks good. Can we add tests for this?

Thanks! Added in 0749a92

@codecov-commenter
Copy link

codecov-commenter commented Jan 9, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 61.89%. Comparing base (8ff9318) to head (c208cb5).
Report is 25 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #100   +/-   ##
=======================================
  Coverage   61.89%   61.89%           
=======================================
  Files          27       27           
  Lines        4091     4091           
=======================================
  Hits         2532     2532           
  Misses       1427     1427           
  Partials      132      132           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mininny mininny force-pushed the feature/mininny/audit-3 branch from 0749a92 to c208cb5 Compare January 9, 2025 02:30
Copy link
Contributor

@refcell refcell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice

@mininny mininny added this pull request to the merge queue Jan 15, 2025
Merged via the queue into master with commit 45c520d Jan 15, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Audit finding grouping for our audit findings
Development

Successfully merging this pull request may close these issues.

4 participants